/**
*******************************************************************
Copyright (C) 2011  Elizar D. Pepino

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
 
*********************************************************************
* @description:	Basic tab plugin for jquery
* @author: Elizar Pepino
* @version: 0.0.1
* @site: http://d2980014.u81.secureserverdot.com/ ( my temporary site domain name already expired )
* 
* @parameters
*	(string)	selectedClass		- a class that will be applied to the selected tab
*	(string)	container			- a container that holds all the content list, or for ajax results
*   (boolean)	isAjax				- determains if ajax will be used ( could be 0,1 or true,false )
*	(string)	effect				- transition effect, defaults to fade ( currently supports "slide" and "fade" )
*	(string)	filters				- an element or selector that will not be included as content items, separated by comma i.e ".li a, div.someclass"
*	(str/int)	speed				- the speed when animating the switch
*	(string)	easing				- the easing that will be used on the animation
*	(func)		callback			- the function that will be called once switching is done
*
**/

jQuery.fn.extend({
	basictab: function(options) {
		return this.each(function() {
			new jQuery.BasicTab(this, options);
		});
	}
});


jQuery.BasicTab = function(target, options) {
	
	// Setup vars
	var _target = $(target);
	var _selectedIndex = 0;
	var firstRun;
	
	// Setup options
	var _options = options || {};
	_options.selectedClass = _options.selectedClass || "selected-tab";
	_options.container = _options.container || ".tab-container";
	_options.effect = _options.effect || "fade";
	_options.easing = _options.easing || "easeInOutExpo";
	_options.isAjax = _options.isAjax || 0;
	_options.filters = _options.filters || "a";
	_options.speed = _options.speed || 700;
	_options.callback = _options.callback || function(){};
	
	// change container's overflow property to "hidden"
	if($(_options.container).css('overflow') != "hidden")
		$(_options.container).css('overflow','hidden');
	
	// do initial switch
	switchTab(_selectedIndex);
	
	// select each tab
	_target.children().each(function(){
	
		// change default cursor to pointer
		var _this = $(this);
		
		_this.css("cursor","pointer").click(function(){
		
			// get index of this target
			var targetIndex = _target.children().index(_this);
			// check if the last selected index is not same as 
			// the current target index
			// then switch to the new target's index
			if(_selectedIndex != targetIndex)
			{
				switchTab(targetIndex);
			}
			// disable defaults
			return false;
			
		});
	});
	
	
	function switchTab(index)
	{
		//	for debugging purposes only
		//	console.log("Selected index = " + index);
		
		var first;
		if(!firstRun){
			firstRun = 1;
			first = true;
		}
		else
		{
			first = false;
		}
		
		// get previous selected tab and remove class
		var previousSelectedTab = _target.children().get(_selectedIndex);
		$(previousSelectedTab).removeClass(_options.selectedClass);
		
		// set new selected and add class
		var selectedTab = _target.children().get(index);
		$(selectedTab).addClass(_options.selectedClass);
		
		// hide current showed element inside the container
		
		if(_options.isAjax)
		{
			// fetch url and insert to the container
			
		}
		else
		{
			
			// switch to the next content
			if($(_options.container).children("div, ul").not($(_options.filters)).length > 1)
			{
				$(_options.container).children("div, ul").not($(_options.filters)).each(function(cIndex){
					
					// hide all tabs first
					$(this).hide();
					
					// then show the selected tab
					if(cIndex == index)
					{
						if(_options.effect == "fade")
						$(this).fadeIn({
							duration: (first)? 10 : _options.speed, 
							easing: (first)? "" : _options.easing, 
							complete: _options.callback
							});
						else if(_options.effect == "slide")
						$(this).slideDown({
							duration: (first)? 10 : _options.speed, 
							easing: (first)? "" : _options.easing, 
							complete: _options.callback
							});
						else
						$(this).fadeIn({
							duration: (first)? 10 : _options.speed, 
							easing: (first)? "" : _options.easing, 
							complete: _options.callback
							});
					}
				});
			}
		}
		
		_selectedIndex = index;
		
		//
	}

}

